package service

import (
	"context"
	"github.com/astaxie/beego/orm"
	"graduation/database"
	"graduation/design"
)

type PermissionService struct {
}

func (self *PermissionService) ListPermission(ctx context.Context, message *design.Empty) (*design.Permission, error) {
	o := orm.NewOrm()
	result := []*database.Permission{}
	_, error := o.QueryTable("permission").All(&result, "Id", "Name", "Code")
	if error != nil {
		return &design.Permission{
			Code: 500,
			Msg:  error.Error(),
			Data: nil,
		}, nil
	} else {
		data := []*design.Permission_Data{}
		for _, value := range result {
			data = append(data, &design.Permission_Data{
				Id:  int32( value.Id),
				Name: value.Name,
				Code: value.Code,
			})
		}
		return &design.Permission{
			Code: 200,
			Msg:  "获取成功",
			Data: data,
		}, nil
	}
}
